package domain.cron;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jsr107cache.Cache;
import net.sf.jsr107cache.CacheException;
import net.sf.jsr107cache.CacheManager;

import domain.data.QnAPersistenceManager;
import domain.model.Domain;
import domain.service.AgeDomain;
import domain.service.AlexaUpdate;
import domain.service.CheckRobotSitemap;
import domain.service.GooglePageRank;
import domain.service.Ip;
import domain.service.SelfSiteUpdate;

@SuppressWarnings("serial")
public class CronUpdateServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		resp.getWriter().println("Hello, world");
		PersistenceManager psm = QnAPersistenceManager.get().getPersistenceManager();
		Query query = psm.newQuery(Domain.class);
		query.setFilter("lastUpdateDate==null");
		query.setRange(0,1);
		@SuppressWarnings("unchecked")
		List<Domain> domains = (List<Domain>) query.execute();
		if(domains.size()>0)
		{
			Domain domain = domains.get(0);
			try {
				new AgeDomain(domain);
			} catch (Exception e) {
				// TODO: handle exception
			}
			try {
				new AlexaUpdate(domain);
			} catch (Exception e) {
				// TODO: handle exception
			}
			try {
				new GooglePageRank(domain);
			} catch (Exception e) {
				// TODO: handle exception
			}
			try {
				new Ip(domain);
			} catch (Exception e) {
				// TODO: handle exception
			}
			try {
				new domain.service.Whois(domain.getName());
			} catch (Exception e) {
				// TODO: handle exception
			}
			try {
				new SelfSiteUpdate(domain);
			} catch (Exception e) {
				// TODO: handle exception
			}
			if(domain.getDescription()!= null && domain.getDescription().getValue().equals("") || domain.getDescription() ==  null)
		    {
		    	domain.setName("www."+domain.getName());
		    	try {
		    		new SelfSiteUpdate(domain);
				} catch (Exception e) {
					// TODO: handle exception
				}
			    domain.setName(domain.getName().replaceAll("www.", ""));
		    }
			psm=JDOHelper.getPersistenceManager(domain);
			psm.currentTransaction().begin();
			psm.makePersistent(domain);
			psm.currentTransaction().commit();
			
			Cache cache=null;
			
	        try {
	            cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
	        } catch (CacheException e) {
	           e.printStackTrace();
	           
	        }
	        
	        String[] bisi = new String[4];	
	        bisi[0] = domain.getName();
	        bisi[1] = domain.getTitle();

	        if(domain.getDescription() != null)
	        	bisi[2] = domain.getDescription().getValue();
	        else 
	        	bisi[2] = domain.getName();
	        	if(domain.getLastUpdateDate() != null)
	        		bisi[3] = domain.getLastUpdateDate().toGMTString();
	        	else
	        		bisi[3] = new Date().toGMTString();
	        if(cache.containsKey("lastPings"))
	        {
	        	@SuppressWarnings("unchecked")
				ArrayList<String[]> lastPing = (ArrayList<String[]>)cache.get("lastPings");
	        	if(lastPing.size()>8)
	        	{
	        		lastPing.remove(0);
	        	}
	        	int check = 0;
	        	for (int i = 0; i < lastPing.size(); i++) {
					if(lastPing.get(i)[0] != null && lastPing.get(i)[0].equals(bisi[0]))
					{
						check = 1;
					}
				}
	        	if(check == 0)
	        		lastPing.add(bisi);
	        		cache.put("lastPings", lastPing);
	        	
	        }
	        else
	        {
	        	ArrayList<String[]> lastPing = new ArrayList<String[]>();
	        	lastPing.add(bisi);
        		cache.put("lastPings", lastPing);
	        }
		}
	}

}
